package com.example.news0515.fragments.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.news0515.Bean.Data;
import com.example.news0515.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.List;

public class MyNewsAdapter extends BaseMultiItemQuickAdapter<Data, BaseViewHolder> {
    public MyNewsAdapter(List<Data> data) {
        super(data);
        addItemType(1, R.layout.mynews_item_a);
        addItemType(2, R.layout.mynews_item_b);
    }

    protected void convert(BaseViewHolder helper, Data item) {
       switch (helper.getItemViewType()){
           case 1:
               helper.setText(R.id.a_title,item.getTitle());
               helper.setText(R.id.a_tv_source,item.getAuthor_name());
               helper.setText(R.id.a_tv_time,item.getDate());
               Bitmap bitmap= setPosterBitmap(item.getThumbnail_pic_s());
               helper.setImageBitmap(R.id.a_iv, bitmap);
               break;
           case 2:
               helper.setText(R.id.b_title,item.getTitle());
               Bitmap bitmap1 = setPosterBitmap(item.getThumbnail_pic_s());
               Bitmap bitmap2 = setPosterBitmap(item.getThumbnail_pic_s02());
               Bitmap bitmap3 = setPosterBitmap(item.getThumbnail_pic_s03());
               helper.setImageBitmap(R.id.b_iv1,bitmap1);
               helper.setImageBitmap(R.id.b_iv2,bitmap2);
               helper.setImageBitmap(R.id.b_iv3,bitmap3);
               break;
       }
    }
    Bitmap setPosterBitmap(String url) {
        final Bitmap[] mBitmap = new Bitmap[1];
        Picasso.get().load(url).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                mBitmap[0] = bitmap;
            }
            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {
            }
            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
        });
        return mBitmap[0];
    }
}
